package cn.witeye.vpais.ga1400.common.kit;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @Title: FileKit
 * @Description: 文件操作工具
 * @Author: Junior Ray
 * @Date: 2020-09-03 21:45
 * @Version: 1.0
 * @Copyright: Copyright (c) 2020 SoftSz All rights reserved
 **/

public class FileKit {
    private static String DOWNLOAD_TEMP_PATH = "/download/temp";

   /**
     *  根据byte数组，生成文件
     * @param bytes byte字节流
     * @param basePath 文件存放目录
     * @param fileName 文件名称(带后缀的)
     * @return
     */
    public static File byteToFile(byte[] bytes, String basePath,String fileName) {
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        String path = "";
        try {
            File dir = new File(basePath);
            if(!dir.exists()&&!dir.isDirectory()){//判断文件目录是否存在
                dir.mkdirs();
            }
            String prefix = fileName.replace("."+suffix,"");
            //创建临时文件的api参数 (文件前缀,文件后缀,存放目录)
            file = File.createTempFile(prefix, "."+suffix, dir);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("创建临时文件失败!" + e.getMessage());
            return null;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        }
    }

    /**
     * @Description: 临时文件下载，使用普通io流方式下载
     * @Author: Junior Ray
     * @Date: 2020/9/10 17:36
     * @param filePath
     * @return:
     */

    public static void downloadFile(HttpServletResponse response,String filePath) throws IOException{

        File file = new File(filePath);
        String filename = file.getName();
        //普通IO流实现下载的功能
        HttpServletResponse res = response; //创建response回应
        res.setContentType("text/html; charset=UTF-8"); //设置编码字符
        res.setContentType("application/x-msdownload"); //设置内容类型
        res.setHeader("Content-disposition", "attachment;filename="+filename);//设置下载的文件名称
        OutputStream out = res.getOutputStream();   //创建页面返回方式为输出流，可弹出下载框

        //创建输入流读取文件
        InputStream is = new FileInputStream(filePath);
        byte[] Buffer = new byte[4096];
        int size = 0;
        while((size=is.read(Buffer)) != -1){
            out.write(Buffer, 0, size);
        }

        //关闭和释放流
        out.flush();
        out.close();
        is.close();
        //删除临时文件
        file.delete();

    }
    /**
     * 将文件转换成byte数组
     * @param file
     * @return
     */
    public static byte[] fileToByte(File file){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1){
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }




}
